﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0827 //2011.03.07 //Begin
namespace InfoJetSoft.Service.Worker
{
    class FormFormatHtmlDocWorker
    {
        public static void formatHtmlDoc(Form form)
        {
            XmlElement root = form.HtmlDoc.DocumentElement;
            string xdBinding = NamespaceUtil.toFullName(form.XdPrefix, JetConstant.XD_binding);

            string datafmtName = NamespaceUtil.toFullName(form.XdPrefix, "datafmt");
            string xctnameName = NamespaceUtil.toFullName(form.XdPrefix, "xctname");

            FormatInfo info = new FormatInfo();
            info.xdBinding = xdBinding;
            info.datafmtName = datafmtName;
            info.xctnameName = xctnameName;
            info.idAttrList = new List<XmlAttribute>();
            info.idTable = new Hashtable();
            info.textareaList = new List<XmlElement>();
            info.inputList = new List<XmlElement>();
            info.selectList = new List<XmlElement>();
            string browserType = form.HttpContext.Request.Browser.Type.ToLower();
            info.isFireFox = browserType.StartsWith("netscape") || browserType.IndexOf("firefox") >= 0;
            //CID 1339 //2012.09.18 //Begin
            info.isIE = form.HttpContext.Request.Browser.Browser.Equals("IE");
            //CID 1339 //2012.09.18 //End
            info.srcAttrNodeList = new List<XmlAttribute>();
            //CID 0741 //2011.03.07 //Begin
            if (ConfigUtil.EnableGrayPlaceholderText())
            {
                info.xdGhosted = NamespaceUtil.toFullName(form.XdPrefix, "ghosted");
            }
            else
            {
                info.xdGhosted = null;
            }
            //CID 0741 //2011.03.08 //End

            loopElement(form, root, info);

            FormFormatDuplicateIdWorker.formatDuplicateId(form, info.idAttrList, info.idTable, info.xctnameName);

            //因为在FormFormatDuplicateIdWorker里还可能修改Id，所以必须在其之后调用FormBuildBrowserInitIdsWorker。
            foreach (XmlElement textAreaElement in info.textareaList)
            {
                FormBuildBrowserInitIdsWorker.addTextAreaId(form, textAreaElement);
                FormDisableNoBindingControlWorker.setDisabledById(textAreaElement);
            }

            //因为在FormFormatDuplicateIdWorker里还可能修改Id，所以必须在其之后调用FormDisableNoBindingControlWorker。
            foreach (XmlElement inputElement in info.inputList)
            {
                //CID 1171 //2012.02.24 //Begin
                string imprecise = inputElement.GetAttribute("imprecise");
                if (imprecise != null && imprecise.Equals("1"))
                {
                    FormBuildBrowserInitIdsWorker.addImpriceDTPickerId(form, inputElement);
                }
                //CID 1171 //2012.02.24 //End
                string type = inputElement.GetAttribute("type");
                if (type.Length <= 0 || type.Equals("text") || type.Equals("radio"))
                {
                    FormDisableNoBindingControlWorker.setDisabledById(inputElement);
                }
                //CID 0236 //2009.03.03 //Begin
                if (type.Equals("checkbox"))
                {
                    FormDisableNoBindingControlWorker.setDisabledOnCheckbox(inputElement);
                }
                //CID 0236 //2009.03.03 //End
            }
            foreach (XmlElement selectElement in info.selectList)
            {
                FormDisableNoBindingControlWorker.setDisabledById(selectElement);
            }

            if (info.isFireFox)
            {
                foreach (XmlAttribute srcAttrNode in info.srcAttrNodeList)
                {
                    FormRemoveEmptySrcWorker.removeEmptySrc(srcAttrNode);
                }
            }
        }

        private static void loopElement(Form form, XmlElement element, FormatInfo info)
        {
            onElement(form, element, info);
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlElement)
                {
                    XmlElement childElement = (XmlElement)node;
                    loopElement(form, childElement, info);
                }
            }
        }

        private static void onElement(Form form, XmlElement element, FormatInfo info)
        {
            XmlAttribute idAttr = element.GetAttributeNode(JetConstant.HTML_id);
            if (idAttr != null)
            {
                info.idAttrList.Add(idAttr);
            }

            if (info.isFireFox)
            {
                XmlAttribute srcAttrNode = element.GetAttributeNode("src");
                if (srcAttrNode != null && srcAttrNode.Value.Length <= 0)
                {
                    info.srcAttrNodeList.Add(srcAttrNode);
                }
            }

            string name = element.Name.ToLower();
            if (name.Equals("input"))
            {
                string type = element.GetAttribute("type").ToLower();
                if (type.Equals("text") || type.Equals("password"))
                {
                    FormFormatTextBoxWorker.formatInputText(form, element, info.xdBinding, info.datafmtName);
                    //CID 0741 //2011.03.07 //Begin
                    if (info.xdGhosted != null)
                    {
                        FormFormatPlaceholderTextWorker.formatPlaceholderText(form, element, info.xdGhosted);
                    }
                    //CID 0741 //2011.03.07 //End
                }
                info.inputList.Add(element);
            }
            else if (name.Equals("textarea"))
            {
                FormFormatTextBoxWorker.formatTextArea(form, element, info.datafmtName, info.xctnameName);
                info.textareaList.Add(element);
            }
            else if (name.Equals("select"))
            {
                info.selectList.Add(element);
            }
            
            //CID 1339 //2012.09.18 //Begin
            if (ConfigUtil.EnableRichText())
            {
                if (name.Equals("span"))
                {
                    string xctname = element.GetAttribute(info.xctnameName);
                    if (xctname != null && xctname.Equals("RichText"))
                    {
                        FormFormatTextBoxWorker.formatRichText(form, element, info);
                    }
                }
            }
            //CID 1339 //2012.09.18 //End
        }
    }

    class FormatInfo
    {
        public string xdBinding { get; set; }
        public string datafmtName { get; set; }
        public string xctnameName { get; set; }
        public List<XmlAttribute> idAttrList { get; set; }
        public Hashtable idTable { get; set; }
        public List<XmlElement> textareaList { get; set; }
        public List<XmlElement> inputList { get; set; }
        public List<XmlElement> selectList { get; set; }
        public bool isFireFox { get; set; }
        public List<XmlAttribute> srcAttrNodeList { get; set; }
        //CID 0741 //2011.03.07 //Begin
        public string xdGhosted { get; set; }
        //CID 0741 //2011.03.07 //End
        //CID 1339 //2012.09.18 //Begin
        public bool isIE { get; set; }
        //CID 1339 //2012.09.18 //End
    }
}
//CID 0827 //2011.03.07 //End
